from addon.Fade.FadeOut import FadeOut
from lib.ZDirection import ZDirection
from lib.ZLabel import ZLabel
from lib.ZLoadJson import ZLoadJson
from lib.res.fun import _create_image_alpha , _cutImg
from lib.res.glo import SRC
from resource.script.Button.lib.BarketBtn import BarketBtn
from resource.script.Button.lib.MenuBtn import MenuBtn
from resource.ui.panels import Barket


class _BtnStart_ ( MenuBtn ) :
	def __init__ ( self , father ) :
		default = _create_image_alpha ( SRC [ 'button' ] [ 'start' ] [ 'default' ] )
		over = _create_image_alpha ( SRC [ 'button' ] [ 'start' ] [ 'over' ] )
		super ( _BtnStart_ , self ).__init__ ( father , default , over )
		self._pos = 0 , 0

	def _action ( self ) :
		from resource.ui.sces import GameScene
		FadeOut ( self.father , GameScene ( ) )


class _BtnExit_ ( MenuBtn ) :
	def __init__ ( self , father ) :
		default = _create_image_alpha ( SRC [ 'button' ] [ 'exit' ] [ 'default' ] )
		over = _create_image_alpha ( SRC [ 'button' ] [ 'exit' ] [ 'over' ] )
		super ( _BtnExit_ , self ).__init__ ( father , default , over )
		self._pos = 0 , 60

	def _action ( self ) :
		from resource.ui.sces import MainScene
		ZDirection ( )._change_scene ( MainScene ( ) )


class _BtnExit_COPY_ ( MenuBtn ) :
	def __init__ ( self , father ) :
		default = _create_image_alpha ( SRC [ 'button' ] [ 'exit' ] [ 'default' ] )
		over = _create_image_alpha ( SRC [ 'button' ] [ 'exit' ] [ 'over' ] )
		super ( _BtnExit_COPY_ , self ).__init__ ( father , default , over )
		self._pos = 0 , 60

	def _action ( self ) :
		exit ( )


class _BtnBackpack_ ( MenuBtn ) :
	def __init__ ( self , father ) :
		default = _create_image_alpha ( SRC [ 'button' ] [ 'brack' ] [ 'default' ] )
		over = _create_image_alpha ( SRC [ 'button' ] [ 'brack' ] [ 'over' ] )
		super ( _BtnBackpack_ , self ).__init__ ( father , default , over )
		self._pos = 45 , 235

	def _action ( self ) :
		from lib.ZScreen import scenes
		for x in scenes :
			x._running = False
		barket = Barket ( self.father.father )
		barket._running = True
		scenes.append ( barket )


class _ItemBtn_Barket ( BarketBtn ) :
	def __init__ ( self , father , item ) :
		from resource.script.Item.res.glo import _i_src
		self.id = int ( item [ 'id' ] )
		self.num = int ( item [ 'num' ] )
		self.data = ZLoadJson ( _i_src [ 'data' ] [ 'item' ] [ self.id ] )
		self.images = _cutImg ( _create_image_alpha ( self.data.get ( ) [ 'icon-big' ] ) )
		image = self.images [ self.data.get ( ) [ "icon-small" ] ]
		super ( _ItemBtn_Barket , self ).__init__ ( father , image ,
		                                            image )

	def _action ( self ) :
		if self.id == 2 :
			from resource.ui.sces import players
			if players [ 0 ]._use_item ( self.id ) :
				players [ 0 ]._add_js ( int ( 10 ) )
				self.father._create ( )

	def draw ( self ) :
		super ( _ItemBtn_Barket , self ).draw ( )
		for x in self.children :
			if isinstance ( x , ZLabel ) :
				if self.num > 1 :
					x.draw ( text = str ( self.num ) )
				else :
					self.children.remove ( x )
